// 云函数入口文件
const cloud = require('wx-server-sdk')
const got = require('got')
let appid = '1109781704'
let secret = 'PnJPKHvehHNoLvFj'

cloud.init() //初始化
// 云函数入口函数
exports.main = async(event, context) => {
  const {
    js_code
  } = event;
  let msgCheckUrl = `https://api.q.qq.com/api/getToken?grant_type=client_credential&appid=${appid}&secret=${secret}` //请求接口的链接
  let checkResponse = await got(msgCheckUrl)
  let res = JSON.parse(checkResponse.body);
  let access_token = res.access_token;
  let url = `https://api.q.qq.com/api/json/template/send?access_token=${access_token}`;
  let sendres = await got(url, {
    heaeder: {
      "content-type": "application/json"
    },
    json: true,
    body: {
      "data": {
        "keyword1": {
          "value": "炉石选牌活动"
        },
        "keyword2": {
          "value": "2019-8-19 19:00:00"
        },
        "keyword3": {
          "value": "北方有佳人"
        },
        "keyword4": {
          "value": "男"
        },
        "keyword5": {
          "value": "2019-8-19 16:28:52"
        }
      },
      "formId": "101fc3e116b641eca03507a94d641f32",
      "page": "pages/result/result",
      "templateId": "6f07db0725727cda13085a456ddfda41",
      "touser": "5F1101F27F68C2331B7EEF0C82448B47"
    }
  })

  return sendres
}